import java.util.ArrayList;
import javax.swing.JTextArea;
import java.io.*;

public class Quest implements Serializable{

	private ArrayList<Entity> item_rewards = new ArrayList<Entity>();
	private ArrayList<Entity> Conditions = new ArrayList<Entity>();
	private Player player;
	private String Quest_Type;
	private int XP_Reward;
	private JTextArea feedback;
	private String questText;
	private String completeText;
	private String Title;
	private String QuestGiverName;
	private String impatient;
	private Character QuestGiver;
	private boolean isGiven;
	
	public Quest(String title, String d, String e, Player a, String b, Entity ... QuestObjects) {
		Title = title;
		isGiven = false;
		player = a;
		Quest_Type = b;
		for (Entity o : QuestObjects) {
			Conditions.add(o);
		}
		questText = d;
		completeText = e;
		feedback = player.getGUI().getFeedbackWindow();
	}
	
	public void setFeedback(JTextArea a) {
		feedback = a;
	}
	
	public void setQuestGiver(Character a) {
		QuestGiverName = a.getName();
		QuestGiver = a;
		questText = (QuestGiverName + " says: " + questText);
		completeText = (QuestGiverName + " says: " + completeText);
		impatient = (QuestGiverName + " says: ");
		if (Quest_Type.equalsIgnoreCase("has")) {
			if (Conditions.size() == 1) {
				impatient = (impatient + "Have you managed to find " + Conditions.get(0).getDisplayName() + " yet?");
			} else {
				impatient = (impatient + "Have you managed to find those items yet?");
			}
		}
	}
	
	public Character getGiver() {
		return QuestGiver;
	}
	
	public String getQuestText() {
		return questText;
	}
	
	public String getCompleteText() {
		return completeText;
	}
	
	public void addRewards(int a, Entity ... Rewards) {
		XP_Reward = a;
		for (Entity i : Rewards) {
			item_rewards.add(i);
		}
	}
	
	public ArrayList<Entity> getRewards() {
		return item_rewards;
	}
	
	public void complete() {
		if (Quest_Type.equalsIgnoreCase("has")) {
			Entity match = null;
			for (Entity b : Conditions) {	
				for (Entity a : player.getInventory()) {
					if (a.getDisplayName().equalsIgnoreCase(b.getDisplayName()) && isGiven == true) {
						match = a;
						break;
					}
				}
				player.giveQuestItem(match, QuestGiver);
			}
		}
		
			for (Entity i : item_rewards) {
				i.setVisible(true);
				player.addEntity(i);
				QuestGiver.getInventory().remove(i);
			}
		player.modifySkillPoints(XP_Reward);
		player.getQuests().remove(this);
	}
	
	public boolean checkComplete() {
		boolean result = false;
		int Count = 0;
		if (Quest_Type.equalsIgnoreCase("has")) {
			for (Entity b : Conditions) {	
				for (Entity a : player.getInventory()) {
					if (a.getDisplayName().equalsIgnoreCase(b.getDisplayName()) && isGiven == true) {
						Count++;
						break;
					} else {
						result = false;
					}
				}
			}
			if (Count == Conditions.size()) {
				result = true;
			} else {
				result = false;
			}
		} else if (Quest_Type.equalsIgnoreCase("killed")) {
			int Counter = 0;
			for (Entity a : Conditions) {
				Character Condition = (Character) a;
				if (Condition.isCorpse() == true) {
					Counter++;
				}
			}
			if (Counter == Conditions.size()) {
				result = true;
			} else {
				result = false;
			}
				
		} else if (Quest_Type.equalsIgnoreCase("speaks")) {
			if (isGiven == true) {
				result = true;
			} else {
				result = false;
			}
		}
		return result;
	}
	
	public void give() {
		player.addQuest(this);
		if (Quest_Type.equalsIgnoreCase("Speaks")) {
			Character TurnIn = (Character) Conditions.get(0);
			TurnIn.getQuests().add(0, this);
		}
		feedback.append(questText);
		isGiven = true;
	}
	
	public String getGiverName() {
		return QuestGiverName;
	}
	
	public String getMissionType() {
		String result = null;
		if (Quest_Type.equalsIgnoreCase("has")) {
			result = "find";
		} else if (Quest_Type.equalsIgnoreCase("speaks")) {
			result = "speak to";
		} else if (Quest_Type.equalsIgnoreCase("killed")) {
			result = "kill";
		}
		return result;
	}
	
	public ArrayList<Entity> getQuestItems() {
		return Conditions;
	}
	
	public boolean isGiven() {
		return isGiven;
	}
	
	public int getXP_Reward() {
		return XP_Reward;
	}
	
	public String getTitle() {
		return Title;
	}
	
	public String getImpatient() {
		return impatient;
	}
	
}
